<?php
/**
 * 后台站点设置模型
 * AdminRole.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * ----------------------------------------------
 * 
 * @package xcs
 * @author Huangdaohai <huangdaohai.2007@163.com>
 * @version $Id$
 * @access public
 */
class AdminMenu extends AdminModel
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return AdminMenu the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{admin_menu}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('menu_name, controller_id, action_id, fid', 'required'),
			array('fid, display', 'numerical', 'integerOnly'=>true),
			array('menu_name, controller_id, action_id', 'length', 'max'=>30),
			array('pids ,module_id ,sort', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('menu_id, menu_name, module_id, controller_id, action_id, fid, display', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'parent'=>array(self::BELONGS_TO,'AdminMenu','fid')
		);
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'menu_id' => 'Menu',
			'menu_name' => '菜单名称',
            'module_id' => '绑定模块',
			'controller_id' => '绑定控制器',
			'action_id' => '绑定方法',
			'fid' => '上级菜单',
			'display' => '是否在菜单栏显示',
            'sort' => '排序'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('menu_id',$this->menu_id);
		$criteria->compare('menu_name',$this->menu_name,true);
		$criteria->compare('module_id',$this->module_id,true);
        $criteria->compare('controller_id',$this->controller_id,true);
		$criteria->compare('action_id',$this->action_id,true);
		$criteria->compare('fid',$this->fid);
		$criteria->compare('display',$this->display);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	protected function afterDelete()
	{
		$next = AdminMenu::model()->find('fid=:fid',array(':fid'=>$this->menu_id));
		if($next){
			foreach ($next as $v){
				AdminMenu::model()->deleteByPk($v->menu_id);
			}
		}
		parent::afterDelete();
	}
    public function saveData($attributes){
        $this->attributes=$attributes;
        if( $this->save() ){
            return $this->menu_id;
        } else {
            return $this->errors;
        }
    }
	/**
	 * AdminMenu::getMenu() 获取所有菜单数据
	 * 
	 * @param string $fid
	 * @param string $display
	 * @return
	 */
	public static function getMenu($fid='',$display=''){
		$where = 'where 1 ';
		if(!empty($fid)){
			$where.=' and fid="'.$fid.'"';
		}
		if(!empty($display)){
			$where.=' and display="'.$display.'"';
		}
		$sql = 'select * from {{admin_menu}} '.$where.' order by menu_id asc';
		$menuAll = Yii::app()->db->createCommand($sql)->queryAll();
		$menu=array();
		$result=array();
		foreach ($menuAll as $v){
			$result[$v['menu_id']]=$v;
		}
		foreach ($result as $k=>$v){
			$fid=$v['fid'];
			if($fid==0){
				$menu[]=&$result[$k];
			}else{
				$result[$fid]['child'][$k]=&$result[$k];//引用当前循环的这一个值，为其找到父级
			}
		}
		return $menu;
	}
	public static function getMenuData($module_name){
		$model=AdminMenu::model()->findAll(
            array('select'=>'*' ,'order'=>'sort desc')
        );
		$arr = array();
		foreach ($model as $v){
			$str_manage = array();
			$str_manage[]=CHtml::link('添加下级菜单',Yii::app()->createUrl($module_name.'/menu/create',array('fid'=>$v->menu_id)));
			if(Yii::app()->user->checkAccess('menu|update')){
				$str_manage[]=CHtml::link('编辑',Yii::app()->createUrl($module_name.'/menu/update',array('id'=>$v->menu_id)));
			}
			if(Yii::app()->user->checkAccess('menu|delete')){
				$str_manage[]=CHtml::link('删除','javascript:void(0);',array('url'=>Yii::app()->createUrl($module_name.'/menu/delete',array('id'=>$v->menu_id)),'class'=>'delete'));
			}
			$arr[]=array(
					'id'=>$v->menu_id,
					'parentid'=>$v->fid,
					'menu_name'=>$v->menu_name,
					'display'=>($v->display==1?'是':'否'),
					'str_manage'=>!empty($str_manage)?join(' ', $str_manage):''
			);
		}
		return $arr;
	}
	
}